package cn.xinfei.xdecision.datasource.mysql;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * 此mysql实例存储配置类信息
 */
@Slf4j
@Configuration
@MapperScan(basePackages = {"cn.xinfei.xdecision.common.dao"},sqlSessionTemplateRef = "sqlSessionTemplate")
public class PrimaryMySQLTemplate {


    @Autowired
    private DefaultDBFieldHandler defaultDBFieldHandler;


    @Bean
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.druid.primary")
    public DruidDataSource druidDataSource() {
        log.info("初始化 PrimaryMySQLTemplate");
        return new DruidDataSource();
    }


    @Bean(name = "sqlSessionFactory")
    @Primary
    public SqlSessionFactory sqlSessionFactory(@Qualifier("druidDataSource") DataSource druidDataSource) throws Exception {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource[] mapperXmlResource = resolver.getResources("classpath*:cn/xinfei/xdecision/common/dao/**/*Mapper.xml");
        bean.setMapperLocations(mapperXmlResource);
        bean.setDataSource(druidDataSource);
        bean.setTypeAliasesPackage("cn.xinfei.xdecision.common.model");
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setMetaObjectHandler(defaultDBFieldHandler);
        bean.setGlobalConfig(globalConfig);
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setCacheEnabled(false);
        bean.setConfiguration(configuration);
        return bean.getObject();
    }


    @Primary
    @Bean(name = "dataSourceTransactionManager")
    public DataSourceTransactionManager dataSourceTransactionManager(@Qualifier("druidDataSource") DruidDataSource defaultDataSource) {
        DataSourceTransactionManager dm = new DataSourceTransactionManager();
        dm.setDataSource(defaultDataSource);
        return dm;
    }

    @Primary
    @Bean(name = "sqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}
